#! /bin/sh

if [ ${#} -eq 1 -a "${1}" = "show" ] ; then
	find . \( -path ./build -prune -o -path ./_aux -prune \) -a -type f -o \( ! -name 'config.hxx' -a \( -name '*.hxx' -o -name '*.cxx' \) \) | xargs grep -n "^#[[:space:]]*define[[:space:]]\+" | grep -v "VCSID" | grep --color "\<M_[A-Z_]\+"
	find . \( -path ./build -prune -o -path ./_aux -prune \) -a -type f -o \( ! -name 'config.hxx' -a \( -name '*.hxx' -o -name '*.cxx' \) \) | xargs grep -n "^#[[:space:]]*define[[:space:]]\+" | grep -v "\<M_\|_HXX_INCLUDED\>" | grep --color " [a-zA-Z_]\+"
	find . \( -path ./build -prune -o -path ./_aux -prune \) -a -type f -o \( ! -name 'config.hxx' -a \( -name '*.hxx' -o -name '*.cxx' \) \) | xargs grep -En --color "(\<reinterpret_cast\>|\<const_cast\>|\<dynamic_cast\>)"
	exit 0
else
	AWK="gawk"
	SOURCES=`find . \( -path ./build -prune -o -path ./_aux -prune \) -a -type f -o -name '*.cxx'`
	HEADERS=`find . \( -path ./build -prune -o -path ./_aux -prune \) -a -type f -o \( ! -name 'config.hxx' -a -name '*.hxx' \)`
	ALL="${SOURCES} ${HEADERS}"

	SOURCES_COUNT=`echo ${SOURCES} | tr " " "\n" | wc -l`
	HEADERS_COUNT=`echo ${HEADERS} | tr " " "\n" | wc -l`

	printf "yaal statistics:\n"
	printf "	implementation files: %5d\n" ${SOURCES_COUNT}
	printf "	header files:      %8d\n" ${HEADERS_COUNT}
	TOTAL=`expr ${SOURCES_COUNT} + ${HEADERS_COUNT}`
	printf "	total:             %8d\n" ${TOTAL}

#	TMP_FILE=`mktemp`
	TMP_FILE=`mktemp -t yaal-stats.XXXXXX`

	cat ${SOURCES} ${HEADERS} | grep -v '^[ 	]*$\|^[ 	]*/\*\|^[ 	]*\*\|^[ 	]*{$\|^#.*include' > ${TMP_FILE}

	LINES=`wc -l ${TMP_FILE} | ${AWK} '{print $1}'`
	LINES=`expr ${LINES} - \( ${TOTAL} \* 21 \)`
	UNIQUE_LINES=`sort -b -u ${TMP_FILE} | wc -l`
	UNIQUE_LINES=`expr ${UNIQUE_LINES} - 21`

	PROG_M="/VCSID|_HXX_INCLUDED\>/{next}/^#[ \t]*define[ \t]+/{print substr(\$0,match(\$0,\"[ \t]M_[A-Z_]*\"),RLENGTH)}"
	PROG_O="/_HXX_INCLUDED\>|\<D_|\<E_|\<M_/{next}/^#[ \t]*define[ \t]+/"

	printf "	lines:             %8d\n" ${LINES}

	printf "	uniqe lines:       %8d\n" ${UNIQUE_LINES}

	CLASSES=`grep '^[[:space:]]*class.*[^;]$' ${TMP_FILE} | grep -o 'class[[:space:]][[:alpha:]]*' | sort -u | wc -l`

	printf "	classes:           %8d\n" ${CLASSES}

	STRUCTURES=`grep '^[[:space:]]*struct.*[^;]$' ${TMP_FILE} | grep -o 'struct[[:space:]][[:alpha:]]*' | sort -u | wc -l`

	printf "	structures:        %8d\n" ${STRUCTURES}

	MACROS=`${AWK} "${PROG_M}" ${TMP_FILE}|sort -u|grep -v '^$'|wc -l`

	printf "	macros:            %8d\n" ${MACROS}

	SNEAKY=`${AWK} "${PROG_O}" ${TMP_FILE}|sort -u|grep -v '^$'|wc -l`

	printf "	sneaky:            %8d\n" ${SNEAKY}

	REINTERPRET_CAST=`grep '\<reinterpret_cast\>' ${TMP_FILE} | wc -l`

	printf "	reinterpret_cast:  %8d\n" ${REINTERPRET_CAST}

	CONST_CAST=`grep '\<const_cast\>' ${TMP_FILE} | wc -l`

	printf "	const_cast:        %8d\n" ${CONST_CAST}

	DYNAMIC_CAST=`grep '\<dynamic_cast\>' ${TMP_FILE} | wc -l`

	printf "	dynamic_cast:      %8d\n" ${DYNAMIC_CAST}

	TYPEDEFS=`grep -c '\<typedef\>' ${TMP_FILE}`

	printf "	typedefs:          %8d\n" ${TYPEDEFS}

	FUNCTIONS=`grep -c ") {" ${TMP_FILE}`
	NON_FUNCTIONS=`grep -c "\(\<if\>\|\<while\>\|\<for\>\|\<switch\>\|\<case\>\).*)[[:space:]][{]" ${TMP_FILE}`

	FUNCTIONS=`expr ${FUNCTIONS} - ${NON_FUNCTIONS}`

	printf "	functions:         %8d\n" ${FUNCTIONS}

	GUARDED_FUNCTIONS=`grep -c "\<M_PROLOG\>" ${TMP_FILE}`

	printf "	guarded functions: %8d\n" ${GUARDED_FUNCTIONS}
fi

if [ -f ${TMP_FILE} ] ; then
	/bin/rm -f ${TMP_FILE}
fi

exit 0

# vim: ts=8
